package terra.network.packet;

import java.util.HashMap;

import org.jboss.netty.buffer.ChannelBuffer;

import terra.network.packet.client.*;
import terra.network.packet.server.*;
import terra.network.packet.server.battle.*;

/*
 * packet id's:
 * 0 Ping
 * 1 RequestLogIn
 * 2 ClientResolution
 * 3 PlayerControl
 * 4 RequestChatMessage
 * 
 * 5 LoginAnswer
 * 6 ObjectDelete
 * 7 ObjectMove
 * 8 ChatMessage
 * 9 PlayerInfo
 * 10 CharInfo
 * 11 StaticInfo
 * 12 TimeInfo
 * 13 ChunkInfo
 * 14 NpcInfo
 * 15 ItemInfo
 * 16 TileInfo
 * 17 InventoryInfo
 * 18 RequestUseItem
 * 19 RequestDropItem
 * 20 StatsInfo
 
 * 21 Attack
 * 22 ArrowInfo
 * 23 ArrowStop
 * 24 RequestCraftItem
 * 25 Ressurection
 * 26 ErrorPacket
 * 27 CraftResult
 * 28 HitInfo
 * 29 LightInfo
 * 30 WaterInfo
 * 31 Blow
 * 32 WeatherInfo
 * 33 DoorInfo
 */

public class PacketHandler {

	public static HashMap<Byte,Packet> packets = new HashMap<Byte,Packet>();
	
	public static Packet getPacketById(byte id){
		return packets.get(id);
	}

	public static boolean containsPacketId(byte id){
		return packets.containsKey(id);
	}

	public static int getPacketLength(byte id){
		return packets.get(id).getLength();
	}
	
	public static void init(){
		registerPacket(new Ping());
		registerPacket(new BigPing());
		registerPacket(new RequestLogIn());	
		registerPacket(new PlayerControl());	
		registerPacket(new ClientResolution());	
		registerPacket(new RequestChatMessage());
		
		registerPacket(new LoginAnswer());	
		registerPacket(new ObjectDelete());
		registerPacket(new ObjectMove());
		registerPacket(new ChatMessage());	
		registerPacket(new PlayerInfo());	
		registerPacket(new CharInfo());	
		registerPacket(new StaticInfo());	
		registerPacket(new TimeInfo());	
		registerPacket(new ChunkInfo());
		registerPacket(new NpcInfo());	
		registerPacket(new ItemInfo());	
		registerPacket(new TileInfo());	
		registerPacket(new InventoryInfo());
		
		registerPacket(new RequestUseItem());	
		registerPacket(new RequestDropItem());	
		
		registerPacket(new Attack());
		registerPacket(new ArrowInfo());	
		registerPacket(new ArrowStop());	
		
		registerPacket(new RequestCraftItem());	
		registerPacket(new Resurection());	
		registerPacket(new ErrorPacket());	
		registerPacket(new CraftResult());	
		registerPacket(new HitInfo());	
		registerPacket(new LightInfo());
		registerPacket(new WaterInfo());	
		registerPacket(new Blow());
		registerPacket(new WeatherInfo());
		registerPacket(new DoorInfo());
	}

	public static void registerPacket(Packet p){
		packets.put(p.getId(), p);
	}
	
	public static Packet readPacket(byte id, ChannelBuffer buffer) throws InstantiationException, IllegalAccessException {
		Packet p = packets.get(id).getClass().newInstance();
		if (p != null)
			p.fromBuffer(buffer);
		return p;
	}
	
	public static Packet readPacket(Packet p, ChannelBuffer buffer) throws InstantiationException, IllegalAccessException {
		Packet packet = p.getClass().newInstance();
		packet.fromBuffer(buffer);
		return packet;
	}
}
